package com.example.fundappcation.weight;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.example.fundappcation.databinding.DialogAddBinding;

import org.jetbrains.annotations.NotNull;

public class AddDialog extends DialogFragment {
    DialogAddBinding mBinding;
    String message;


    //初始化dialog

    @NonNull
    @NotNull
    @Override
    public Dialog onCreateDialog(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getContext());
        Window window = dialog.getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.gravity = Gravity.CENTER;
        mBinding = DialogAddBinding.inflate(getLayoutInflater());
        dialog.addContentView(mBinding.getRoot(), layoutParams);
        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();

        initView();
        initData();
    }

    private void initData() {
        mBinding.tvDialogConfirm.setOnClickListener(o -> {
            message = mBinding.edtDialogName.getText().toString();
            if (message.length() > 0 && mOnItemClickList != null) {
                mOnItemClickList.onConfirm(AddDialog.this, message);
            }
        });
        mBinding.tvDialogCancel.setOnClickListener(o -> {
            if (mOnItemClickList != null) {
                mOnItemClickList.onCancel(AddDialog.this);
            }
        });
    }


    @Override
    public void show(@NonNull @NotNull FragmentManager manager, @Nullable String tag) {
        super.show(manager, tag);

    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    private void initView() {
    }

    //以被废弃
//    @Override
//    public void onActivityCreated(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
//        super.onActivityCreated(savedInstanceState);
//    }

    private onItemClickList mOnItemClickList;


    public interface onItemClickList {
        void onConfirm(DialogFragment dialogFragment, String message);

        void onCancel(DialogFragment dialogFragment);
    }

    public onItemClickList getOnItemClickList() {
        return mOnItemClickList;
    }

    public void setOnItemClickList(onItemClickList mOnItemClickList) {
        this.mOnItemClickList = mOnItemClickList;
    }
}
